#include <iostream>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "Task.hpp"
#include "Tool.hpp"
using namespace std;

//非阻塞轮询方案
int main()
{
    Tool t;
    t.PushFunc(DownLoad);
    t.PushFunc(PrintLog);
    t.PushFunc(FlushDate);
    pid_t id = fork();
    if(id == 0)
    {
        int cnt = 10;
        while(cnt--)
        {
            printf("子进程在运行: %d\n", cnt);
            sleep(1);
        }
        exit(0);
    }
    while(1)
    {

        pid_t rid = waitpid(id, NULL, WNOHANG);
        if(rid == id)
        {
            printf("wait childproc success!\n");
            break;
        }
        else if(rid == 0)
        {
            printf("childproc not quit!\n");
            t.Execute();
            sleep(1);
        }
        else
        {
            printf("wait error!\n");
            break;
        }
    }
    return 0;
}
